package org.openjsonp.json;

import java.util.LinkedHashMap;

import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;

/**
 * Implementation class of the interface {@link javax.json.JsonObject
 * JsonObject}.
 * 
 */
public class JsonObjectImpl extends LinkedHashMap<String, JsonValue> implements JsonObject {

	private static final long serialVersionUID = 7216115731444121469L;

	/**
	 * Construts a JsonObject.
	 */
	public JsonObjectImpl() {
		super();
	}

	@Override
	public ValueType getValueType() {
		return JsonValue.ValueType.OBJECT;
	}

	@Override
	public JsonArray getJsonArray(String name) throws ClassCastException {
		return (JsonArray) get(name);
	}

	@Override
	public JsonObject getJsonObject(String name) throws ClassCastException {
		return (JsonObject) get(name);
	}

	@Override
	public JsonNumber getJsonNumber(String name) throws ClassCastException {
		return (JsonNumber) get(name);
	}

	@Override
	public JsonString getJsonString(String name) throws ClassCastException {
		return (JsonString) get(name);
	}

	@Override
	public String getString(String name) throws NullPointerException, ClassCastException {
		return getJsonString(name).getString();
	}

	@Override
	public String getString(String name, String defaultValue) {
		JsonValue jsonValue = get(name);
		String stringValue = defaultValue;

		if (JsonValue.ValueType.STRING.equals(jsonValue.getValueType())) {
			stringValue = ((JsonString) jsonValue).getString();
		}

		return stringValue;
	}

	@Override
	public int getInt(String name) throws NullPointerException, ClassCastException {
		return getJsonNumber(name).intValue();
	}

	@Override
	public int getInt(String name, int defaultValue) {
		JsonValue value = get(name);
		int intValue = defaultValue;

		if (ValueType.NUMBER.equals(value.getValueType())) {
			intValue = ((JsonNumber) value).intValue();
		}

		return intValue;
	}

	@Override
	public boolean getBoolean(String name) throws NullPointerException, ClassCastException {
		JsonValue value = get(name);
		boolean booleanValue;

		if (JsonValue.TRUE.equals(value)) {
			booleanValue = true;
		}
		else if (JsonValue.FALSE.equals(value)) {
			booleanValue = false;
		}
		else {
			throw new ClassCastException(
					"Expected class type javax.json.JsonValue.ValueType.TRUE or javax.json.JsonValue.ValueType.FALSE. Actual : "
							+ value.getClass());
		}

		return booleanValue;
	}

	@Override
	public boolean getBoolean(String name, boolean defaultValue) {
		JsonValue value = get(name);
		boolean booleanValue = defaultValue;

		if (JsonValue.TRUE.equals(value)) {
			booleanValue = true;
		}
		else if (JsonValue.FALSE.equals(value)) {
			booleanValue = false;
		}

		return booleanValue;
	}

	@Override
	public boolean isNull(String name) throws NullPointerException {
		JsonValue value = get(name);
		return JsonValue.NULL.equals(value);
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		for (Entry<String, JsonValue> entry : entrySet()) {
			buffer.append(" \"");
			buffer.append(entry.getKey());
			buffer.append("\"");
			buffer.append(": ");
			buffer.append(entry.getValue());
			buffer.append(",");
		}
		buffer.delete(buffer.length() - 1, buffer.length());
		buffer.append(" }");
		return buffer.toString();
	}
}
